<?php
/**
 * Created by PhpStorm.
 * User: longli
 * VX: isa1589518286
 * Date: 2021/09/29
 * Time: 23:38
 * @link http://www.lmterp.cn
 */

namespace app\admin\controller\order;

use app\admin\controller\BaseController;
use app\common\library\Tools;
use app\common\model\OrdersRules;
use app\common\model\OrdersRulesGroup;
use app\common\service\product\ProductService;

/**
 * 规则分组
 * Class GroupController
 * @package app\admin\controller\cha
 */
class GroupController extends BaseController
{
    /**
     * 分组首页
     * @date 2021/08/30
     * @author longli
     */
    public function index()
    {
        $this->assign("is_show_add", $this->user->hasPermissions("/order/group/add"));
        if($this->request->isAjax())
        {
            $rules = OrdersRulesGroup::with(['rules'])->where("ref_type", OrdersRulesGroup::REF_TYPE_WAREHOUSE);
            $this->searchModel($rules, [
                'eq' => ['group_id', 'status'],
                'times' => ['create_time'],
            ]);
            $rules->order("sort desc");
            $limit = $this->getPageSize();
            $rules = $rules->paginate($limit);
            $this->assign("is_show_delete", $this->user->hasPermissions("/order/group/delete"));
            $this->assign('list', $rules->getCollection());
            $this->assign('page', $rules->render());
            return $this->fetch('lists');
        }
        $this->assign("groups", OrdersRulesGroup::refType(OrdersRulesGroup::REF_TYPE_WAREHOUSE)->select());
        $this->assign("status", ['否', '是']);
        return $this->fetch('index');
    }

    /**
     * 查看规则组详情
     * @date 2021/09/01
     * @author longli
     */
    public function info()
    {
        $gid = $this->request->get('group_id');
        if(empty($gid) || !($group = OrdersRulesGroup::get($gid))) $this->error('非法操作');
        $this->assign('group', $group);
        return $this->fetch('info');
    }

    /**
     * 添加规则分组
     * @date 2021/08/31
     * @author longli
     */
    public function add()
    {
        $gid = $this->request->get("group_id");
        if(!empty($gid) && ($group = OrdersRulesGroup::get($gid)))
        {
            $this->assign("group", $group);
        }
        $this->assign("rule_type", OrdersRules::$RULE_TYPE);
        $this->assign("condition_type", OrdersRules::$CONDITION_TYPE);
        $this->assign("match_type", OrdersRules::$MATCH_TYPE);
        $this->assign("match_type_def", OrdersRules::MATCH_TYPE_OR);
        return $this->fetch('add');
    }

    /**
     * 保存规则分组
     * @date 2021/08/31
     * @author longli
     */
    public function save()
    {
        $rules = [];
        if($this->request->isPost() && !$this->request->has('group_id'))
        {
            $json = $this->request->getContent();
            if(!Tools::isJson($json, $data)) $this->error("非法请求");
            $data = ProductService::getInstance()->parseRequestData($data);
            $group = $data['group'];
            $rules = $data['rule'];
            $service = \app\common\service\orders\ValidateRulesService::getInstance();
            if(($gMsg = $service->validateGroup($group)) !== true) $this->error($gMsg);
            if(($rMsg = $service->validateRules($rules)) !== true) $this->error($rMsg);
            if(!isset($group['status'])) $group['status'] = OrdersRulesGroup::IS_NO;
            $group['ref_type'] = OrdersRulesGroup::REF_TYPE_WAREHOUSE;
        }
        else
        {
            $group = $this->request->only(['group_id', 'status', 'sort'], 'request');
        }
        \app\common\service\orders\OrderService::getInstance()->addGroup($group, $rules)
            ? $this->success("操作成功")
            : $this->error("操作失败");
    }

    /**
     * 删除规则分组
     * @date 2021/08/31
     * @author longli
     */
    public function delete()
    {
        $ids = $this->request->get("ids");
        if(empty($ids)) $this->error("非法操作");
        OrdersRulesGroup::destroy($ids);
        OrdersRules::destroy(function($query)use($ids)
        {
            $query->whereIn("group_id", $ids);
        });
        $this->success("删除成功");
    }
}